package com.feature50.makeover.utils;

import java.io.File;
import java.io.InputStream;

public class ResourceLoader {
    public enum Type { Form, Stylesheet, Font, Image, None }

    private static ResourceLoader instance;

    public ClassLoader getClassLoader() {
        return getClass().getClassLoader();
    }

    public InputStream getInputStream(Type type, String name) {
        String dir;
        switch (type) {
            case Form:
                dir = "forms";
                break;
            case Stylesheet:
                dir = "css";
                break;
            case Font:
                dir = "fonts";
                break;
            case Image:
                dir = "images";
                break;
            case None:
                dir = null;
                break;
            default:
                throw new IllegalArgumentException(String.format("Type %1$s unknown", type));
        }

        return getInputStream(dir, name);
    }

    public InputStream getInputStream(String resourcesDirName, String name) {
        String dir = (resourcesDirName != null) ? resourcesDirName + File.separatorChar + name : name;
        return getClassLoader().getResourceAsStream(dir);
    }

    public static synchronized ResourceLoader getInstance() {
        if (instance == null) {
            instance = new ResourceLoader();
        }

        return instance;
    }
}